<?php
namespace cms\modules\frontend\controllers;

use Yii;
use yii\helpers\Html;
use yii\db\Expression;
use cms\controllers\BasicController;
use app\models\Poster;
use common\component\tool\Tool;
use common\component\exception\ErrMap;


class PosterController extends BasicController
{
    /*跳转 记录点击量 接口和标签里面的地址需要经过这里跳转，这样才能计算点击量*/
    public function actionJump()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $poster = Poster::find()
                        ->select(['id', 'outside', 'url', 'start_time', 'end_time', 'add_time', 'display'])
                        ->where('id = :id', [':id' => $id])
                        ->one();
            // P($poster);
            if(!is_null($poster) and $poster->display == 2 and (strtotime($poster->start_time) < time() and strtotime($poster->end_time) > time())){
                // P($poster);
                $isMobile = Tool::isMobile();//判断是否手机访问
                if($isMobile){
                	Poster::updateAllCounters(['mobile_click_count' => 1], 'id = :id', [':id' => $poster->id]);
                }else{
                	Poster::updateAllCounters(['pc_click_count' => 1], 'id = :id', [':id' => $poster->id]);
                }
                if($poster->outside == 2){//外链
                	header("Location: " . $poster->url);
                }else{
                	header("Location: /" . $poster->url);
                }
                return false;
            };
        };
        header("Location: /");
    } 


}
